﻿#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#else
using System.Windows;
using System.Windows.Controls;
#endif

namespace FreshMeat.Xaml
{
   /// <summary>
   /// Arranges items using a <see cref="Grid"/> with the specified number of rows and columns as the items host.
   /// The item container is of type <see cref="ItemsGridItem"/>.
   /// </summary>
   public class ItemsGrid : ItemsControl
   {
#if !SILVERLIGHT && !NETFX_CORE
      static ItemsGrid()
      {
         DefaultStyleKeyProperty.OverrideMetadata(typeof(ItemsGrid), new FrameworkPropertyMetadata(typeof(ItemsGrid)));
      }
#endif

      public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register(
         "Columns",
         typeof(int),
         typeof(ItemsGrid),
         new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));

      public readonly static DependencyProperty RowsProperty = DependencyProperty.Register(
         "Rows",
         typeof(int),
         typeof(ItemsGrid),
         new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));

      public readonly static DependencyProperty MinColumnWidthProperty = DependencyProperty.Register(
         "MinColumnWidth",
         typeof(double),
         typeof(ItemsGrid),
         new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));

      public readonly static DependencyProperty MinRowHeightProperty = DependencyProperty.Register(
         "MinRowHeight",
         typeof(double),
         typeof(ItemsGrid),
         new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));

      public readonly static DependencyProperty ColumnWidthProperty = DependencyProperty.Register(
         "ColumnWidth",
         typeof(GridLength),
         typeof(ItemsGrid),
         new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));

      public readonly static DependencyProperty RowHeightProperty = DependencyProperty.Register(
         "RowHeight",
         typeof(GridLength),
         typeof(ItemsGrid),
         new FrameworkPropertyMetadata(GridLength.Auto, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));

#if SILVERLIGHT
      public readonly static DependencyProperty ItemContainerStyleProperty = DependencyProperty.Register(
         "ItemContainerStyle",
         typeof(Style),
         typeof(ItemsGrid),
         new PropertyMetadata(null));
#endif

      public ItemsGrid()
      {
         DefaultStyleKey = typeof(ItemsGrid);
      }

      protected override DependencyObject GetContainerForItemOverride()
      {
         var container = new ItemsGridItem();

         if (ItemContainerStyle != null)
            container.Style = ItemContainerStyle;

         return container;
      }

      public int Columns
      {
         get { return (int)GetValue(ColumnsProperty); }
         set { SetValue(ColumnsProperty, value); }
      }

      public int Rows
      {
         get { return (int)GetValue(RowsProperty); }
         set { SetValue(RowsProperty, value); }
      }

      public double MinColumnWidth
      {
         get { return (double)GetValue(MinColumnWidthProperty); }
         set { SetValue(MinColumnWidthProperty, value); }
      }

      public double MinRowHeight
      {
         get { return (double)GetValue(MinRowHeightProperty); }
         set { SetValue(MinRowHeightProperty, value); }
      }

      public GridLength ColumnWidth
      {
         get { return (GridLength)GetValue(ColumnWidthProperty); }
         set { SetValue(ColumnWidthProperty, value); }
      }

      public GridLength RowHeight
      {
         get { return (GridLength)GetValue(RowHeightProperty); }
         set { SetValue(RowHeightProperty, value); }
      }

#if SILVERLIGHT
      public Style ItemContainerStyle
      {
         get { return (Style)GetValue(ItemContainerStyleProperty); }
         set { SetValue(ItemContainerStyleProperty, value); }
      }
#endif
   }

   public class ItemsGridItem : ContentPresenter
   {
   }
}
